<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>docker常用命令 - Foryun - Share</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Foryun" /><meta name="description" content="容器生命周期管理 run docker run: 创建一个新的容器并运行一个命令 语法 1 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明： -a stdin: 指定标准输入输出内容类型，可选 STDIN/STDOUT/STDERR 三项； -d: 后台运" /><meta name="keywords" content="Blog, IT, Share, Record" />






<meta name="generator" content="Hugo 0.81.0 with theme even" />


<link rel="canonical" href="http://foryun.com.cn/post/docker/docker%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<link href="/sass/main.min.af7fd1da18d66c2b017df5b4cae508ef44cfcac3fb4c7c7a327fe4f4f9e28b08.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="docker常用命令" />
<meta property="og:description" content="容器生命周期管理 run docker run: 创建一个新的容器并运行一个命令 语法 1 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明： -a stdin: 指定标准输入输出内容类型，可选 STDIN/STDOUT/STDERR 三项； -d: 后台运" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://foryun.com.cn/post/docker/docker%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2020-09-22T00:00:00&#43;00:00" />
<meta property="article:modified_time" content="2020-09-22T00:00:00&#43;00:00" />

<meta itemprop="name" content="docker常用命令">
<meta itemprop="description" content="容器生命周期管理 run docker run: 创建一个新的容器并运行一个命令 语法 1 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明： -a stdin: 指定标准输入输出内容类型，可选 STDIN/STDOUT/STDERR 三项； -d: 后台运"><meta itemprop="datePublished" content="2020-09-22T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2020-09-22T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="6283">
<meta itemprop="keywords" content="docker," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="docker常用命令"/>
<meta name="twitter:description" content="容器生命周期管理 run docker run: 创建一个新的容器并运行一个命令 语法 1 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明： -a stdin: 指定标准输入输出内容类型，可选 STDIN/STDOUT/STDERR 三项； -d: 后台运"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Foryun</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archives</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Categories</li>
      </a><a href="/diary/">
        <li class="mobile-menu-item">Diary</li>
      </a><a href="/record/">
        <li class="mobile-menu-item">Record</li>
      </a><a href="/game/">
        <li class="mobile-menu-item">Game</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">About</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Foryun</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archives</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Categories</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/diary/">Diary</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/record/">Record</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/game/">Game</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">About</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">docker常用命令</h1>

      <div class="post-meta">
        <span class="post-time"> 2020-09-22 </span>
        <div class="post-category">
            <a href="/categories/docker/"> docker </a>
            </div>
          <span class="more-meta"> 约 6283 字 </span>
          <span class="more-meta"> 预计阅读 13 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#容器生命周期管理">容器生命周期管理</a>
          <ul>
            <li><a href="#run">run</a></li>
            <li><a href="#startstoprestart">start/stop/restart</a></li>
            <li><a href="#kill">kill</a></li>
            <li><a href="#rm">rm</a></li>
            <li><a href="#pauseunpause">pause/unpause</a></li>
            <li><a href="#create">create</a></li>
            <li><a href="#exec">exec</a></li>
          </ul>
        </li>
        <li><a href="#容器操作">容器操作</a>
          <ul>
            <li><a href="#ps">ps</a></li>
            <li><a href="#inspect">inspect</a></li>
            <li><a href="#top">top</a></li>
            <li><a href="#attach">attach</a></li>
            <li><a href="#events">events</a></li>
            <li><a href="#logs">logs</a></li>
            <li><a href="#wait">wait</a></li>
            <li><a href="#export">export</a></li>
            <li><a href="#port">port</a></li>
          </ul>
        </li>
        <li><a href="#容器rootfs命令">容器rootfs命令</a>
          <ul>
            <li><a href="#commit">commit</a></li>
            <li><a href="#cp">cp</a></li>
            <li><a href="#diff">diff</a></li>
          </ul>
        </li>
        <li><a href="#镜像仓库">镜像仓库</a>
          <ul>
            <li><a href="#login">login</a></li>
            <li><a href="#pull">pull</a></li>
            <li><a href="#push">push</a></li>
            <li><a href="#search">search</a></li>
          </ul>
        </li>
        <li><a href="#本地镜像管理">本地镜像管理</a>
          <ul>
            <li><a href="#images">images</a></li>
            <li><a href="#rmi">rmi</a></li>
            <li><a href="#tag">tag</a></li>
            <li><a href="#build">build</a></li>
            <li><a href="#history">history</a></li>
            <li><a href="#save">save</a></li>
            <li><a href="#load">load</a></li>
            <li><a href="#import">import</a></li>
          </ul>
        </li>
        <li><a href="#infoversion">info|version</a>
          <ul>
            <li><a href="#info">info</a></li>
            <li><a href="#version">version</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="容器生命周期管理">容器生命周期管理</h2>
<h3 id="run">run</h3>
<blockquote>
<p><strong>docker run:</strong>  创建一个新的容器并运行一个命令</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-a stdin:</strong>  指定标准输入输出内容类型，可选 STDIN/STDOUT/STDERR 三项；</li>
<li><strong>-d:</strong>  后台运行容器，并返回容器ID；</li>
<li><strong>-i:</strong>  以交互模式运行容器，通常与 -t 同时使用；</li>
<li><strong>-P:</strong>  随机端口映射，容器内部端口<strong>随机</strong>映射到主机的端口</li>
<li><strong>-p:</strong>  指定端口映射，格式为：<strong>主机(宿主)端口:容器端口</strong></li>
<li><strong>-t:</strong>  为容器重新分配一个伪输入终端，通常与 -i 同时使用；</li>
<li><strong>&ndash;name=&ldquo;nginx-lb&rdquo;:</strong>  为容器指定一个名称；</li>
<li><strong>&ndash;dns 8.8.8.8:</strong>  指定容器使用的DNS服务器，默认和宿主一致；</li>
<li><strong>&ndash;dns-search example.com:</strong>  指定容器DNS搜索域名，默认和宿主一致；</li>
<li><strong>-h &ldquo;mars&rdquo;:</strong>  指定容器的hostname；</li>
<li><strong>-e username=&ldquo;ritchie&rdquo;:</strong>  设置环境变量；</li>
<li><strong>&ndash;env-file=[]:</strong>  从指定文件读入环境变量；</li>
<li><strong>&ndash;cpuset=&ldquo;0-2&rdquo; or &ndash;cpuset=&ldquo;0,1,2&rdquo;:</strong>  绑定容器到指定CPU运行；</li>
<li><strong>-m:</strong> 设置容器使用内存最大值；</li>
<li><strong>&ndash;net=&ldquo;bridge&rdquo;:</strong>  指定容器的网络连接类型，支持 bridge/host/none/container: 四种类型；</li>
<li><strong>&ndash;link=[]:</strong>  添加链接到另一个容器；</li>
<li><strong>&ndash;expose=[]:</strong>  开放一个端口或一组端口；</li>
<li><strong>&ndash;volume , -v:</strong>  绑定一个卷</li>
</ul>
<p><strong>实例</strong></p>
<p>使用docker镜像nginx:latest以后台模式启动一个容器,并将容器命名为mynginx。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker run --name mynginx -d nginx:latest
</code></pre></td></tr></table>
</div>
</div><p>使用镜像nginx:latest以后台模式启动一个容器,并将容器的80端口映射到主机随机端口。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker run -P -d nginx:latest
</code></pre></td></tr></table>
</div>
</div><p>使用镜像 nginx:latest，以后台模式启动一个容器,将容器的 80 端口映射到主机的 80 端口,主机的目录 /data 映射到容器的 /data。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker run -p 80:80 -v /data:/data -d nginx:latest
</code></pre></td></tr></table>
</div>
</div><p>绑定容器的 8080 端口，并将其映射到本地主机 127.0.0.1 的 80 端口上。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
</code></pre></td></tr></table>
</div>
</div><p>使用镜像nginx:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker run -it nginx:latest /bin/bash
root@b8573233d675:/#
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="startstoprestart">start/stop/restart</h3>
<blockquote>
<p><strong>docker start</strong> :  启动一个或多个已经被停止的容器</p>
<p><strong>docker stop</strong> :  停止一个运行中的容器</p>
<p><strong>docker restart</strong> :  重启容器</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker start [OPTIONS] CONTAINER [CONTAINER...]
docker stop [OPTIONS] CONTAINER [CONTAINER...]
docker restart [OPTIONS] CONTAINER [CONTAINER...]
</code></pre></td></tr></table>
</div>
</div><p><strong>实例</strong></p>
<p>启动已被停止的容器myrunoob</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker start myrunoob
</code></pre></td></tr></table>
</div>
</div><p>停止运行中的容器myrunoob</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker stop myrunoob
</code></pre></td></tr></table>
</div>
</div><p>重启容器myrunoob</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker restart myrunoob
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="kill">kill</h3>
<blockquote>
<p><strong>docker kill</strong> : 杀掉一个运行中的容器。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker kill [OPTIONS] CONTAINER [CONTAINER...]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-s:</strong>  向容器发送一个信号</li>
</ul>
<p>实例</p>
<p>杀掉运行中的容器mynginx</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker kill -s KILL mynginx
mynginx
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="rm">rm</h3>
<blockquote>
<p><strong>docker rm：</strong> 删除一个或多个容器。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker rm [OPTIONS] CONTAINER [CONTAINER...]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-f:</strong>  通过 SIGKILL 信号强制删除一个运行中的容器。</li>
<li><strong>-l:</strong>  移除容器间的网络连接，而非容器本身。</li>
<li><strong>-v:</strong>  删除与容器关联的卷。</li>
</ul>
<p><strong>实例</strong></p>
<p>强制删除容器 db01、db02：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker rm -f db01 db02
</code></pre></td></tr></table>
</div>
</div><p>移除容器 nginx01 对容器 db01 的连接，连接名 db：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker rm -l db 
</code></pre></td></tr></table>
</div>
</div><p>删除容器 nginx01, 并删除容器挂载的数据卷：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker rm -v nginx01
</code></pre></td></tr></table>
</div>
</div><p>删除所有已经停止的容器：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker rm $(docker ps -a -q)
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="pauseunpause">pause/unpause</h3>
<blockquote>
<p><strong>docker pause</strong>  : 暂停容器中所有的进程。</p>
<p><strong>docker unpause</strong> : 恢复容器中所有的进程。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker pause [OPTIONS] CONTAINER [CONTAINER...]
docker unpause [OPTIONS] CONTAINER [CONTAINER...]
</code></pre></td></tr></table>
</div>
</div><p><strong>实例</strong></p>
<p>暂停数据库容器db01提供服务。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker pause db01
</code></pre></td></tr></table>
</div>
</div><p>恢复数据库容器db01提供服务。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker unpause db01
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="create">create</h3>
<blockquote>
<p>**docker create：**创建一个新的容器但不启动它</p>
<p>用法同 <a href="https://www.runoob.com/docker/docker-run-command.html">docker run</a></p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
</code></pre></td></tr></table>
</div>
</div><p>语法同 <a href="https://www.runoob.com/docker/docker-run-command.html">docker run</a></p>
<p><strong>实例</strong></p>
<p>使用docker镜像nginx:latest创建一个容器,并将容器命名为myrunoob</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker create  --name myrunoob  nginx:latest      
09b93464c2f75b7b69f83d56a9cfc23ceb50a48a9db7652ee4c27e3e2cb1961f
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="exec">exec</h3>
<blockquote>
<p>**docker exec：**在运行的容器中执行命令</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-d:</strong> 分离模式: 在后台运行</li>
<li><strong>-i:</strong> 即使没有附加也保持STDIN 打开</li>
<li><strong>-t:</strong> 分配一个伪终端</li>
</ul>
<p><strong>实例</strong></p>
<p>在容器 mynginx 中以交互模式执行容器内 /root/runoob.sh 脚本:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker exec -it mynginx /bin/sh /root/runoob.sh
http://www.runoob.com/
</code></pre></td></tr></table>
</div>
</div><p>在容器 mynginx 中开启一个交互模式的终端:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker exec -i -t  mynginx /bin/bash
root@b1a0703e41e7:/#
</code></pre></td></tr></table>
</div>
</div><p>也可以通过 <strong>docker ps -a</strong> 命令查看已经在运行的容器，然后使用容器 ID 进入容器。</p>
<p>查看已经在运行的容器 ID：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback"># docker ps -a 
...
9df70f9a0714        openjdk             &#34;/usercode/script.sh…&#34; 
...
</code></pre></td></tr></table>
</div>
</div><p>第一列的 9df70f9a0714 就是容器 ID。</p>
<p>通过 exec 命令对指定的容器执行 bash:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback"># docker exec -it 9df70f9a0714 /bin/bash
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h2 id="容器操作">容器操作</h2>
<h3 id="ps">ps</h3>
<blockquote>
<p><strong>docker ps:</strong>  列出容器</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker ps [OPTIONS]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li>
<p><strong>-a:</strong> 显示所有的容器，包括未运行的。</p>
</li>
<li>
<p><strong>-f:</strong> 根据条件过滤显示的内容。</p>
</li>
<li>
<p><strong>&ndash;format:</strong> 指定返回值的模板文件。</p>
</li>
<li>
<p><strong>-l:</strong> 显示最近创建的容器。</p>
</li>
<li>
<p><strong>-n:</strong> 列出最近创建的n个容器。</p>
</li>
<li>
<p><strong>&ndash;no-trunc:</strong> 不截断输出。</p>
</li>
<li>
<p><strong>-q:</strong> 静默模式，只显示容器编号。</p>
</li>
<li>
<p><strong>-s:</strong> 显示总的文件大小。</p>
</li>
</ul>
<p><strong>实例</strong></p>
<p>列出所有在运行的容器信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker ps
CONTAINER ID   IMAGE          COMMAND                ...  PORTS                    NAMES
09b93464c2f7   nginx:latest   &#34;nginx -g &#39;daemon off&#34; ...  80/tcp, 443/tcp          myrunoob
96f7f14e99ab   mysql:5.6      &#34;docker-entrypoint.sh&#34; ...  0.0.0.0:3306-&gt;3306/tcp   mymysql
</code></pre></td></tr></table>
</div>
</div><p>输出详情介绍：</p>
<p><strong>CONTAINER ID:</strong>  容器 ID。</p>
<p><strong>IMAGE:</strong>  使用的镜像。</p>
<p><strong>COMMAND:</strong>  启动容器时运行的命令。</p>
<p><strong>CREATED:</strong>  容器的创建时间。</p>
<p><strong>STATUS:</strong>  容器状态。</p>
<p>状态有7种：</p>
<ul>
<li>created（已创建）</li>
<li>restarting（重启中）</li>
<li>running（运行中）</li>
<li>removing（迁移中）</li>
<li>paused（暂停）</li>
<li>exited（停止）</li>
<li>dead（死亡）</li>
</ul>
<p><strong>PORTS:</strong>  容器的端口信息和使用的连接类型（tcp\udp）。</p>
<p><strong>NAMES:</strong>  自动分配的容器名称。</p>
<p>列出最近创建的5个容器信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker ps -n 5
CONTAINER ID        IMAGE               COMMAND                   CREATED           
09b93464c2f7        nginx:latest        &#34;nginx -g &#39;daemon off&#34;    2 days ago   ...     
b8573233d675        nginx:latest        &#34;/bin/bash&#34;               2 days ago   ...     
b1a0703e41e7        nginx:latest        &#34;nginx -g &#39;daemon off&#34;    2 days ago   ...    
f46fb1dec520        5c6e1090e771        &#34;/bin/sh -c &#39;set -x \t&#34;   2 days ago   ...   
a63b4a5597de        860c279d2fec        &#34;bash&#34;                    2 days ago   ...
</code></pre></td></tr></table>
</div>
</div><p>列出所有创建的容器ID。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker ps -a -q
09b93464c2f7
b8573233d675
b1a0703e41e7
f46fb1dec520
a63b4a5597de
6a4aa42e947b
de7bb36e7968
43a432b73776
664a8ab1a585
ba52eb632bbd
...
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="inspect">inspect</h3>
<blockquote>
<p><strong>docker inspect:</strong>  获取容器/镜像的元数据。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker inspect [OPTIONS] NAME|ID [NAME|ID...]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-f:</strong> 指定返回值的模板文件。</li>
<li><strong>-s:</strong> 显示总的文件大小。</li>
<li><strong>&ndash;type:</strong> 为指定类型返回JSON。</li>
</ul>
<p><strong>实例</strong></p>
<p>获取镜像mysql:5.6的元信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker inspect mysql:5.6
[
    {
        &#34;Id&#34;: &#34;sha256:2c0964ec182ae9a045f866bbc2553087f6e42bfc16074a74fb820af235f070ec&#34;,
        &#34;RepoTags&#34;: [
            &#34;mysql:5.6&#34;
        ],
        &#34;RepoDigests&#34;: [],
        &#34;Parent&#34;: &#34;&#34;,
        &#34;Comment&#34;: &#34;&#34;,
        &#34;Created&#34;: &#34;2016-05-24T04:01:41.168371815Z&#34;,
        &#34;Container&#34;: &#34;e0924bc460ff97787f34610115e9363e6363b30b8efa406e28eb495ab199ca54&#34;,
        &#34;ContainerConfig&#34;: {
            &#34;Hostname&#34;: &#34;b0cf605c7757&#34;,
            &#34;Domainname&#34;: &#34;&#34;,
            &#34;User&#34;: &#34;&#34;,
            &#34;AttachStdin&#34;: false,
            &#34;AttachStdout&#34;: false,
            &#34;AttachStderr&#34;: false,
            &#34;ExposedPorts&#34;: {
                &#34;3306/tcp&#34;: {}
            },
...
</code></pre></td></tr></table>
</div>
</div><p>获取正在运行的容器mymysql的 IP。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker inspect --format=&#39;{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}&#39; mymysql
172.17.0.3
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="top">top</h3>
<blockquote>
<p><strong>docker top:</strong> 查看容器中运行的进程信息，支持 ps 命令参数。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker top [OPTIONS] CONTAINER [ps OPTIONS]
</code></pre></td></tr></table>
</div>
</div><p>容器运行时不一定有/bin/bash终端来交互执行top命令，而且容器还不一定有top命令，可以使用docker top来实现查看container中正在运行的进程。</p>
<p><strong>实例</strong></p>
<p>查看容器mymysql的进程信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~/mysql$ docker top mymysql
UID    PID    PPID    C      STIME   TTY  TIME       CMD
999    40347  40331   18     00:58   ?    00:00:02   mysqld
</code></pre></td></tr></table>
</div>
</div><p>查看所有运行容器的进程信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">for i in  `docker ps |grep Up|awk &#39;{print $1}&#39;`;do echo \ &amp;&amp;docker top $i; done
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="attach">attach</h3>
<blockquote>
<p><strong>docker attach:</strong> 连接到正在运行中的容器。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker attach [OPTIONS] CONTAINER
</code></pre></td></tr></table>
</div>
</div><p>要attach上去的容器必须正在运行，可以同时连接上同一个container来共享屏幕（与screen命令的attach类似）。</p>
<p>官方文档中说attach后可以通过CTRL-C来detach，但实际上经过我的测试，如果container当前在运行bash，CTRL-C自然是当前行的输入，没有退出；如果container当前正在前台运行进程，如输出nginx的access.log日志，CTRL-C不仅会导致退出容器，而且还stop了。这不是我们想要的，detach的意思按理应该是脱离容器终端，但容器依然运行。好在attach是可以带上&ndash;sig-proxy=false来确保CTRL-D或CTRL-C不会关闭容器。</p>
<p><strong>实例</strong></p>
<p>容器mynginx将访问日志指到标准输出，连接到容器查看访问信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker attach --sig-proxy=false mynginx
192.168.239.1 - - [10/Jul/2016:16:54:26 +0000] &#34;GET / HTTP/1.1&#34; 304 0 &#34;-&#34; &#34;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36&#34; &#34;-&#34;
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="events">events</h3>
<blockquote>
<p><strong>docker events:</strong>  从服务器获取实时事件</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker events [OPTIONS]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li>**-f：**根据条件过滤事件；</li>
<li>**&ndash;since：**从指定的时间戳后显示所有事件;</li>
<li>**&ndash;until：**流水时间显示到指定的时间为止；</li>
</ul>
<p><strong>实例</strong></p>
<p>显示docker 2016年7月1日后的所有事件。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~/mysql$ docker events  --since=&#34;1467302400&#34;
2016-07-08T19:44:54.501277677+08:00 network connect 66f958fd13dc4314ad20034e576d5c5eba72e0849dcc38ad9e8436314a4149d4 (container=b8573233d675705df8c89796a2c2687cd8e36e03646457a15fb51022db440e64, name=bridge, type=bridge)
2016-07-08T19:44:54.723876221+08:00 container start b8573233d675705df8c89796a2c2687cd8e36e03646457a15fb51022db440e64 (image=nginx:latest, name=elegant_albattani)
2016-07-08T19:44:54.726110498+08:00 container resize b8573233d675705df8c89796a2c2687cd8e36e03646457a15fb51022db440e64 (height=39, image=nginx:latest, name=elegant_albattani, width=167)
2016-07-08T19:46:22.137250899+08:00 container die b8573233d675705df8c89796a2c2687cd8e36e03646457a15fb51022db440e64 (exitCode=0, image=nginx:latest, name=elegant_albattani)
...
</code></pre></td></tr></table>
</div>
</div><p>显示docker 镜像为mysql:5.6 2016年7月1日后的相关事件。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~/mysql$ docker events -f &#34;image&#34;=&#34;mysql:5.6&#34; --since=&#34;1467302400&#34; 
2016-07-11T00:38:53.975174837+08:00 container start 96f7f14e99ab9d2f60943a50be23035eda1623782cc5f930411bbea407a2bb10 (image=mysql:5.6, name=mymysql)
2016-07-11T00:51:17.022572452+08:00 container kill 96f7f14e99ab9d2f60943a50be23035eda1623782cc5f930411bbea407a2bb10 (image=mysql:5.6, name=mymysql, signal=9)
2016-07-11T00:51:17.132532080+08:00 container die 96f7f14e99ab9d2f60943a50be23035eda1623782cc5f930411bbea407a2bb10 (exitCode=137, image=mysql:5.6, name=mymysql)
2016-07-11T00:51:17.514661357+08:00 container destroy 96f7f14e99ab9d2f60943a50be23035eda1623782cc5f930411bbea407a2bb10 (image=mysql:5.6, name=mymysql)
2016-07-11T00:57:18.551984549+08:00 container create c8f0a32f12f5ec061d286af0b1285601a3e33a90a08ff1706de619ac823c345c (image=mysql:5.6, name=mymysql)
2016-07-11T00:57:18.557405864+08:00 container attach c8f0a32f12f5ec061d286af0b1285601a3e33a90a08ff1706de619ac823c345c (image=mysql:5.6, name=mymysql)
2016-07-11T00:57:18.844134112+08:00 container start c8f0a32f12f5ec061d286af0b1285601a3e33a90a08ff1706de619ac823c345c (image=mysql:5.6, name=mymysql)
2016-07-11T00:57:19.140141428+08:00 container die c8f0a32f12f5ec061d286af0b1285601a3e33a90a08ff1706de619ac823c345c (exitCode=1, image=mysql:5.6, name=mymysql)
2016-07-11T00:58:05.941019136+08:00 container destroy c8f0a32f12f5ec061d286af0b1285601a3e33a90a08ff1706de619ac823c345c (image=mysql:5.6, name=mymysql)
2016-07-11T00:58:07.965128417+08:00 container create a404c6c174a21c52f199cfce476e041074ab020453c7df2a13a7869b48f2f37e (image=mysql:5.6, name=mymysql)
2016-07-11T00:58:08.188734598+08:00 container start a404c6c174a21c52f199cfce476e041074ab020453c7df2a13a7869b48f2f37e (image=mysql:5.6, name=mymysql)
2016-07-11T00:58:20.010876777+08:00 container top a404c6c174a21c52f199cfce476e041074ab020453c7df2a13a7869b48f2f37e (image=mysql:5.6, name=mymysql)
2016-07-11T01:06:01.395365098+08:00 container top a404c6c174a21c52f199cfce476e041074ab020453c7df2a13a7869b48f2f37e (image=mysql:5.6, name=mymysql)
</code></pre></td></tr></table>
</div>
</div><p>如果指定的时间是到秒级的，需要将时间转成时间戳。如果时间为日期的话，可以直接使用，如&ndash;since=&ldquo;2016-07-01&rdquo;。</p>
</blockquote>
<h3 id="logs">logs</h3>
<blockquote>
<p><strong>docker logs:</strong>  获取容器的日志</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker logs [OPTIONS] CONTAINER
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-f:</strong>  跟踪日志输出</li>
<li><strong>&ndash;since:</strong> 显示某个开始时间的所有日志</li>
<li><strong>-t:</strong>  显示时间戳</li>
<li><strong>&ndash;tail:</strong> 仅列出最新N条容器日志</li>
</ul>
<p><strong>实例</strong></p>
<p>跟踪查看容器mynginx的日志输出。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker logs -f mynginx
192.168.239.1 - - [10/Jul/2016:16:53:33 +0000] &#34;GET / HTTP/1.1&#34; 200 612 &#34;-&#34; &#34;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36&#34; &#34;-&#34;
2016/07/10 16:53:33 [error] 5#5: *1 open() &#34;/usr/share/nginx/html/favicon.ico&#34; failed (2: No such file or directory), client: 192.168.239.1, server: localhost, request: &#34;GET /favicon.ico HTTP/1.1&#34;, host: &#34;192.168.239.130&#34;, referrer: &#34;http://192.168.239.130/&#34;
192.168.239.1 - - [10/Jul/2016:16:53:33 +0000] &#34;GET /favicon.ico HTTP/1.1&#34; 404 571 &#34;http://192.168.239.130/&#34; &#34;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36&#34; &#34;-&#34;
192.168.239.1 - - [10/Jul/2016:16:53:59 +0000] &#34;GET / HTTP/1.1&#34; 304 0 &#34;-&#34; &#34;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36&#34; &#34;-&#34;
...
</code></pre></td></tr></table>
</div>
</div><p>查看容器mynginx从2016年7月1日后的最新10条日志。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker logs --since=&#34;2016-07-01&#34; --tail=10 mynginx
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="wait">wait</h3>
<blockquote>
<p><strong>docker wait:</strong>  阻塞运行直到容器停止，然后打印出它的退出代码。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker wait [OPTIONS] CONTAINER [CONTAINER...]
</code></pre></td></tr></table>
</div>
</div><p><strong>实例</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker wait CONTAINER
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="export">export</h3>
<blockquote>
<p><strong>docker export:</strong> 将文件系统作为一个tar归档文件导出到STDOUT。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker export [OPTIONS] CONTAINER
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-o:</strong> 将输入内容写到文件。</li>
</ul>
<p><strong>实例</strong></p>
<p>将id为a404c6c174a2的容器按日期保存为tar文件。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker export -o mysql-`date +%Y%m%d`.tar a404c6c174a2
runoob@runoob:~$ ls mysql-`date +%Y%m%d`.tar
mysql-20160711.tar
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="port">port</h3>
<blockquote>
<p><strong>docker port:</strong> 列出指定的容器的端口映射，或者查找将PRIVATE_PORT NAT到面向公众的端口。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]]
</code></pre></td></tr></table>
</div>
</div><p><strong>实例</strong></p>
<p>查看容器mynginx的端口映射情况。</p>
</blockquote>
<h2 id="容器rootfs命令">容器rootfs命令</h2>
<h3 id="commit">commit</h3>
<blockquote>
<p><strong>docker commit:</strong> 从容器创建一个新的镜像。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li>
<p><strong>-a:</strong> 提交的镜像作者；</p>
</li>
<li>
<p><strong>-c:</strong> 使用Dockerfile指令来创建镜像；</p>
</li>
<li>
<p><strong>-m:</strong> 提交时的说明文字；</p>
</li>
<li>
<p><strong>-p:</strong> 在commit时，将容器暂停。</p>
</li>
</ul>
<p><strong>实例</strong></p>
<p>将容器a404c6c174a2 保存为新的镜像,并添加提交人信息和说明信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker commit -a &#34;runoob.com&#34; -m &#34;my apache&#34; a404c6c174a2  mymysql:v1 
sha256:37af1236adef1544e8886be23010b66577647a40bc02c0885a6600b33ee28057
runoob@runoob:~$ docker images mymysql:v1
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mymysql             v1                  37af1236adef        15 seconds ago      329 MB
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="cp">cp</h3>
<blockquote>
<p><strong>docker cp:</strong> 用于容器与主机之间的数据拷贝。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-L:</strong> 保持源目标中的链接</li>
</ul>
<p><strong>实例</strong></p>
<p>将主机/www/runoob目录拷贝到容器96f7f14e99ab的/www目录下。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker cp /www/runoob 96f7f14e99ab:/www/
</code></pre></td></tr></table>
</div>
</div><p>将主机/www/runoob目录拷贝到容器96f7f14e99ab中，目录重命名为www。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker cp /www/runoob 96f7f14e99ab:/www
</code></pre></td></tr></table>
</div>
</div><p>将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker cp  96f7f14e99ab:/www /tmp/
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="diff">diff</h3>
<blockquote>
<p><strong>docker diff:</strong>  检查容器里文件结构的更改。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker diff [OPTIONS] CONTAINER
</code></pre></td></tr></table>
</div>
</div><p><strong>实例</strong></p>
<p>查看容器mymysql的文件结构更改。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker diff mymysql
A /logs
A /mysql_data
C /run
C /run/mysqld
A /run/mysqld/mysqld.pid
A /run/mysqld/mysqld.sock
C /tmp
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h2 id="镜像仓库">镜像仓库</h2>
<h3 id="login">login</h3>
<blockquote>
<p><strong>docker login:</strong>  登陆到一个Docker镜像仓库，如果未指定镜像仓库地址，默认为官方仓库 Docker Hub</p>
<p><strong>docker logout:</strong>  登出一个Docker镜像仓库，如果未指定镜像仓库地址，默认为官方仓库 Docker Hub</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker login [OPTIONS] [SERVER]
docker logout [OPTIONS] [SERVER]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-u:</strong> 登陆的用户名</li>
<li><strong>-p:</strong> 登陆的密码</li>
</ul>
<p><strong>实例</strong></p>
<p>登陆到Docker Hub</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker login -u 用户名 -p 密码
</code></pre></td></tr></table>
</div>
</div><p>登出Docker Hub</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker logout
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="pull">pull</h3>
<blockquote>
<p><strong>docker pull:</strong>  从镜像仓库中拉取或者更新指定镜像</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker pull [OPTIONS] NAME[:TAG|@DIGEST]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li>
<p><strong>-a:</strong> 拉取所有 tagged 镜像</p>
</li>
<li>
<p><strong>&ndash;disable-content-trust:</strong> 忽略镜像的校验,默认开启</p>
</li>
</ul>
<p><strong>实例</strong></p>
<p>从Docker Hub下载java最新版镜像。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker pull java
</code></pre></td></tr></table>
</div>
</div><p>从Docker Hub下载REPOSITORY为java的所有镜像。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker pull -a java
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="push">push</h3>
<blockquote>
<p><strong>docker push:</strong>  将本地的镜像上传到镜像仓库,要先登陆到镜像仓库</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker push [OPTIONS] NAME[:TAG]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>&ndash;disable-content-trust:</strong> 忽略镜像的校验,默认开启</li>
</ul>
<p><strong>实例</strong></p>
<p>上传本地镜像myapache:v1到镜像仓库中。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker push myapache:v1
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="search">search</h3>
<blockquote>
<p><strong>docker search:</strong>  从Docker Hub查找镜像</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker search [OPTIONS] TERM
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>&ndash;automated:</strong> 只列出 automated build类型的镜像；</li>
<li><strong>&ndash;no-trunc:</strong> 显示完整的镜像描述；</li>
<li><strong>-s:</strong> 列出收藏数不小于指定值的镜像。</li>
</ul>
<p><strong>实例</strong></p>
<p>从Docker Hub查找所有镜像名包含java，并且收藏数大于10的镜像</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker search -s 10 java
NAME                  DESCRIPTION                           STARS   OFFICIAL   AUTOMATED
java                  Java is a concurrent, class-based...   1037    [OK]       
anapsix/alpine-java   Oracle Java 8 (and 7) with GLIBC ...   115                [OK]
develar/java                                                 46                 [OK]
isuper/java-oracle    This repository contains all java...   38                 [OK]
lwieske/java-8        Oracle Java 8 Container - Full + ...   27                 [OK]
nimmis/java-centos    This is docker images of CentOS 7...   13                 [OK]
</code></pre></td></tr></table>
</div>
</div><p>参数说明：</p>
<p><strong>NAME:</strong>  镜像仓库源的名称</p>
<p><strong>DESCRIPTION:</strong>  镜像的描述</p>
<p><strong>OFFICIAL:</strong>  是否 docker 官方发布</p>
<p><strong>stars:</strong>  类似 Github 里面的 star，表示点赞、喜欢的意思。</p>
<p><strong>AUTOMATED:</strong>  自动构建。</p>
</blockquote>
<h2 id="本地镜像管理">本地镜像管理</h2>
<h3 id="images">images</h3>
<blockquote>
<p><strong>docker images:</strong>  列出本地镜像。</p>
<p>语法</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker images [OPTIONS] [REPOSITORY[:TAG]]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li>
<p><strong>-a:</strong> 列出本地所有的镜像（含中间映像层，默认情况下，过滤掉中间映像层）；</p>
</li>
<li>
<p><strong>&ndash;digests:</strong> 显示镜像的摘要信息；</p>
</li>
<li>
<p><strong>-f:</strong> 显示满足条件的镜像；</p>
</li>
<li>
<p><strong>&ndash;format:</strong> 指定返回值的模板文件；</p>
</li>
<li>
<p><strong>&ndash;no-trunc:</strong> 显示完整的镜像信息；</p>
</li>
<li>
<p><strong>-q:</strong> 只显示镜像ID。</p>
</li>
</ul>
<p><strong>实例</strong></p>
<p>查看本地镜像列表。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
mymysql                 v1                  37af1236adef        5 minutes ago       329 MB
runoob/ubuntu           v4                  1c06aa18edee        2 days ago          142.1 MB
&lt;none&gt;                  &lt;none&gt;              5c6e1090e771        2 days ago          165.9 MB
httpd                   latest              ed38aaffef30        11 days ago         195.1 MB
alpine                  latest              4e38e38c8ce0        2 weeks ago         4.799 MB
mongo                   3.2                 282fd552add6        3 weeks ago         336.1 MB
redis                   latest              4465e4bcad80        3 weeks ago         185.7 MB
php                     5.6-fpm             025041cd3aa5        3 weeks ago         456.3 MB
python                  3.5                 045767ddf24a        3 weeks ago         684.1 MB
...
</code></pre></td></tr></table>
</div>
</div><p>列出本地镜像中REPOSITORY为ubuntu的镜像列表。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">root@runoob:~# docker images  ubuntu
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              14.04               90d5884b1ee0        9 weeks ago         188 MB
ubuntu              15.10               4e3b13c8a266        3 months ago        136.3 MB
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="rmi">rmi</h3>
<blockquote>
<p><strong>docker rmi:</strong>  删除本地一个或多少镜像。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker rmi [OPTIONS] IMAGE [IMAGE...]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li>
<p><strong>-f:</strong> 强制删除；</p>
</li>
<li>
<p><strong>&ndash;no-prune:</strong> 不移除该镜像的过程镜像，默认移除；</p>
</li>
</ul>
<p><strong>实例</strong></p>
<p>强制删除本地镜像 runoob/ubuntu:v4。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">root@runoob:~# docker rmi -f runoob/ubuntu:v4
Untagged: runoob/ubuntu:v4
Deleted: sha256:1c06aa18edee44230f93a90a7d88139235de12cd4c089d41eed8419b503072be
Deleted: sha256:85feb446e89a28d58ee7d80ea5ce367eebb7cec70f0ec18aa4faa874cbd97c73
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="tag">tag</h3>
<blockquote>
<p><strong>docker tag:</strong>  标记本地镜像，将其归入某一仓库。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]
</code></pre></td></tr></table>
</div>
</div><p><strong>实例</strong></p>
<p>将镜像ubuntu:15.10标记为 runoob/ubuntu:v3 镜像。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">root@runoob:~# docker tag ubuntu:15.10 runoob/ubuntu:v3
root@runoob:~# docker images   runoob/ubuntu:v3
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
runoob/ubuntu       v3                  4e3b13c8a266        3 months ago        136.3 MB
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="build">build</h3>
<blockquote>
<p><strong>docker build</strong> 命令用于使用 Dockerfile 创建镜像。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker build [OPTIONS] PATH | URL | -
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>&ndash;build-arg=[]:</strong> 设置镜像创建时的变量；</li>
<li><strong>&ndash;cpu-shares:</strong> 设置 cpu 使用权重；</li>
<li><strong>&ndash;cpu-period:</strong> 限制 CPU CFS周期；</li>
<li><strong>&ndash;cpu-quota:</strong> 限制 CPU CFS配额；</li>
<li><strong>&ndash;cpuset-cpus:</strong> 指定使用的CPU id；</li>
<li><strong>&ndash;cpuset-mems:</strong> 指定使用的内存 id；</li>
<li><strong>&ndash;disable-content-trust:</strong> 忽略校验，默认开启；</li>
<li><strong>-f:</strong> 指定要使用的Dockerfile路径；</li>
<li><strong>&ndash;force-rm:</strong> 设置镜像过程中删除中间容器；</li>
<li><strong>&ndash;isolation:</strong> 使用容器隔离技术；</li>
<li><strong>&ndash;label=[]:</strong> 设置镜像使用的元数据；</li>
<li><strong>-m:</strong> 设置内存最大值；</li>
<li><strong>&ndash;memory-swap:</strong> 设置Swap的最大值为内存+swap，&quot;-1&quot;表示不限swap；</li>
<li><strong>&ndash;no-cache:</strong> 创建镜像的过程不使用缓存；</li>
<li><strong>&ndash;pull:</strong> 尝试去更新镜像的新版本；</li>
<li><strong>&ndash;quiet, -q:</strong> 安静模式，成功后只输出镜像 ID；</li>
<li><strong>&ndash;rm:</strong> 设置镜像成功后删除中间容器；</li>
<li><strong>&ndash;shm-size:</strong> 设置/dev/shm的大小，默认值是64M；</li>
<li><strong>&ndash;ulimit:</strong> Ulimit配置。</li>
<li><strong>&ndash;tag, -t:</strong>  镜像的名字及标签，通常 name:tag 或者 name 格式；可以在一次构建中为一个镜像设置多个标签。</li>
<li><strong>&ndash;network:</strong>  默认 default。在构建期间设置RUN指令的网络模式</li>
</ul>
<p><strong>实例</strong></p>
<p>使用当前目录的 Dockerfile 创建镜像，标签为 runoob/ubuntu:v1。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker build -t runoob/ubuntu:v1 . 
</code></pre></td></tr></table>
</div>
</div><p>使用URL <strong>github.com/creack/docker-firefox</strong> 的 Dockerfile 创建镜像。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker build github.com/creack/docker-firefox
</code></pre></td></tr></table>
</div>
</div><p>也可以通过 -f Dockerfile 文件的位置：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ docker build -f /path/to/a/Dockerfile .
</code></pre></td></tr></table>
</div>
</div><p>在 Docker 守护进程执行 Dockerfile 中的指令前，首先会对 Dockerfile 进行语法检查，有语法错误时会返回：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ docker build -t test/myapp .
Sending build context to Docker daemon 2.048 kB
Error response from daemon: Unknown instruction: RUNCMD
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="history">history</h3>
<blockquote>
<p><strong>docker history:</strong>  查看指定镜像的创建历史。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker history [OPTIONS] IMAGE
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li>
<p><strong>-H:</strong> 以可读的格式打印镜像大小和日期，默认为true；</p>
</li>
<li>
<p><strong>&ndash;no-trunc:</strong> 显示完整的提交记录；</p>
</li>
<li>
<p><strong>-q:</strong> 仅列出提交记录ID。</p>
</li>
</ul>
<p><strong>实例</strong></p>
<p>查看本地镜像runoob/ubuntu:v3的创建历史。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">root@runoob:~# docker history runoob/ubuntu:v3
IMAGE             CREATED           CREATED BY                                      SIZE      COMMENT
4e3b13c8a266      3 months ago      /bin/sh -c #(nop) CMD [&#34;/bin/bash&#34;]             0 B                 
&lt;missing&gt;         3 months ago      /bin/sh -c sed -i &#39;s/^#\s*\(deb.*universe\)$/   1.863 kB            
&lt;missing&gt;         3 months ago      /bin/sh -c set -xe   &amp;&amp; echo &#39;#!/bin/sh&#39; &gt; /u   701 B               
&lt;missing&gt;         3 months ago      /bin/sh -c #(nop) ADD file:43cb048516c6b80f22   136.3 MB
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="save">save</h3>
<blockquote>
<p><strong>docker save:</strong>  将指定镜像保存成 tar 归档文件。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker save [OPTIONS] IMAGE [IMAGE...]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS 说明：</p>
<ul>
<li><strong>-o:</strong> 输出到的文件。</li>
</ul>
<p><strong>实例</strong></p>
<p>将镜像 runoob/ubuntu:v3 生成 my_ubuntu_v3.tar 文档</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker save -o my_ubuntu_v3.tar runoob/ubuntu:v3
runoob@runoob:~$ ll my_ubuntu_v3.tar
-rw------- 1 runoob runoob 142102016 Jul 11 01:37 my_ubuntu_v3.ta
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="load">load</h3>
<blockquote>
<p><strong>docker load:</strong>  导入使用 <a href="https://www.runoob.com/docker/docker-save-command.html">docker save</a> 命令导出的镜像。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker load [OPTIONS]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS 说明：</p>
<ul>
<li>
<p><strong>&ndash;input , -i:</strong>  指定导入的文件，代替 STDIN。</p>
</li>
<li>
<p><strong>&ndash;quiet , -q:</strong>  精简输出信息。</p>
</li>
</ul>
<p><strong>实例</strong></p>
<p>导入镜像：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

$ docker load &lt; busybox.tar.gz

Loaded image: busybox:latest
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              769b9341d937        7 weeks ago         2.489 MB

$ docker load --input fedora.tar

Loaded image: fedora:rawhide

Loaded image: fedora:20

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              769b9341d937        7 weeks ago         2.489 MB
fedora              rawhide             0d20aec6529d        7 weeks ago         387 MB
fedora              20                  58394af37342        7 weeks ago         385.5 MB
fedora              heisenbug           58394af37342        7 weeks ago         385.5 MB
fedora              latest              58394af37342        7 weeks ago         385.5 MB
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="import">import</h3>
<blockquote>
<p><strong>docker import:</strong>  从归档文件中创建镜像。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li>
<p><strong>-c:</strong> 应用docker 指令创建镜像；</p>
</li>
<li>
<p><strong>-m:</strong> 提交时的说明文字；</p>
</li>
</ul>
<p><strong>实例</strong></p>
<p>从镜像归档文件my_ubuntu_v3.tar创建镜像，命名为runoob/ubuntu:v4</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">runoob@runoob:~$ docker import  my_ubuntu_v3.tar runoob/ubuntu:v4  
sha256:63ce4a6d6bc3fabb95dbd6c561404a309b7bdfc4e21c1d59fe9fe4299cbfea39
runoob@runoob:~$ docker images runoob/ubuntu:v4
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
runoob/ubuntu       v4                  63ce4a6d6bc3        20 seconds ago      142.1 MB
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h2 id="infoversion">info|version</h2>
<h3 id="info">info</h3>
<blockquote>
<p>docker info : 显示 Docker 系统信息，包括镜像和容器数。。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker info [OPTIONS]
</code></pre></td></tr></table>
</div>
</div><p><strong>实例</strong></p>
<p>查看docker系统信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ docker info
Containers: 12
Images: 41
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 66
 Dirperm1 Supported: false
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.13.0-32-generic
Operating System: Ubuntu 14.04.1 LTS
CPUs: 1
Total Memory: 1.954 GiB
Name: iZ23mtq8bs1Z
ID: M5N4:K6WN:PUNC:73ZN:AONJ:AUHL:KSYH:2JPI:CH3K:O4MK:6OCX:5OYW
</code></pre></td></tr></table>
</div>
</div></blockquote>
<h3 id="version">version</h3>
<blockquote>
<p>docker version :显示 Docker 版本信息。</p>
<p><strong>语法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker version [OPTIONS]
</code></pre></td></tr></table>
</div>
</div><p>OPTIONS说明：</p>
<ul>
<li><strong>-f:</strong> 指定返回值的模板文件。</li>
</ul>
<p><strong>实例</strong></p>
<p>显示 Docker 版本信息。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ docker version
Client:
 Version:      1.8.2
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   0a8c2e3
 Built:        Thu Sep 10 19:19:00 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.8.2
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   0a8c2e3
 Built:        Thu Sep 10 19:19:00 UTC 2015
 OS/Arch:      linux/amd64
</code></pre></td></tr></table>
</div>
</div></blockquote>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Foryun</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2020-09-22
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/docker/">docker</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/docker/docker-compose/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">docker-compose</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/share/mac%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E8%AE%BE%E7%BD%AE/">
            <span class="next-text nav-default">mac环境变量设置</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/docker/docker%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" class="leancloud_visitors" data-flag-title="docker常用命令">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'insXINA0D2ccQdcEfEcMSjgn-gzGzoHsz',
        appKey: 'HEAEkloDNn3aFg4Yr1W4iGM3',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:1186976350@qq.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/DanicCheng" class="iconfont icon-github" title="github"></a>
      <a href="https://space.bilibili.com/2920221?spm_id_from=333.788.b_765f7570696e666f.2" class="iconfont icon-bilibili" title="bilibili"></a>
  
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2017 - 
    2021<span class="heart"><i class="iconfont icon-heart"></i></span><span>Foryun</span>
  </span>

  <span class="beian">
    
    备案号：<a href="https://beian.miit.gov.cn" target="_blank">粤ICP备18045119号</a>
    
  </span>

</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?57ed19d6fdd95893edd119b86ac49dab";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>

<script id="baidu_push">
  (function(){
    if (window.location.hostname === 'localhost') return;
    var bp = document.createElement('script'); bp.async = true;
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
      bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
      bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
  })();
</script>




</body>
</html>
